//Custom Exhaust class
Exhaust = function()
{
	Sim.Object.call(this);
}

goog.inherits( Exhaust, Sim.Object );
Exhaust.prototype.Class = 'Exhaust';
Exhaust.prototype.init = function(param)
{
	this.setObject3D(new THREE.Object3D);
	
	this.initParticles();

	this.clock = new THREE.Clock();
}

Exhaust.prototype.initParticles = function()
{
	var sphereRadius = 1;
	
	var particleCount = 100;
	var particles = new THREE.BufferGeometry();
	var vertices = [],velocity = [];
	// var pMaterial = new THREE.PointsMaterial({
	// 		color: 0xffffff,
	// 		size: 1,
	// 		opacity:.05,
	// 		transparent:true,
	// 		map: new THREE.TextureLoader().load('./res/image/smoke-2.png')
	// 	});

	this.pMaterial = new THREE.ShaderMaterial( {

		uniforms: {
			color: { value: new THREE.Color( 0xffffff ) },
			pointTexture: { value: new THREE.TextureLoader().load( "./res/image/smoke-2.png" ) },
			time: {value: 0.1},
		},
		vertexShader: `
			
			attribute float velocity;	

			uniform float time;

			void main() {

				vec3 pos = vec3(position.x + position.y * 0.5 * sin(time), position.y, position.z); 

				vec4 mvPosition = modelViewMatrix * vec4( pos, 1.0 );

				gl_PointSize = 40.0;

				gl_Position = projectionMatrix * mvPosition;

			}

		`,
		fragmentShader: `
			uniform vec3 color;
			uniform sampler2D pointTexture;

			void main() {

				gl_FragColor = vec4( color, 1.0 );

				gl_FragColor = gl_FragColor * texture2D( pointTexture, gl_PointCoord );

				// if(gl_FragColor.w < 0.1 ) discard;
			}
		`,

		transparent: true

	} );


	for(var p = 0; p < particleCount; p++) {

		var radius =  sphereRadius*0.05;
		var angle = Math.random() * (Math.PI * 2)
		var pX = Math.sin(angle) * radius,
			pY = Math.random() * 1,
			pZ = 0;
		vertices.push(pX,pY,pZ);
			
		velocity.push( Math.random()-0.5,
			Math.random()*0.25,
			0 );

	}

	particles.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
	particles.setAttribute( 'velocity', new THREE.Float32BufferAttribute( velocity, 3 ) );
	// create the particle system
	var particleSystem = new THREE.Points(
		particles,
		this.pMaterial);
	particleSystem.position.set(0,-0.5,0);

	particleSystem.sortParticles = false;

	// add it to the scene
	this.object3D.add(particleSystem);
	
	this.particleCount = particleCount;
	this.particles = particles;
	this.particleSystem = particleSystem;
	this.sphereRadius = sphereRadius;
}

Exhaust.prototype.update = function(timestamp)
{
	//update particles
	// var delta = 5 * this.clock.getDelta();
	this.pMaterial.uniforms.time.value += timestamp;
	// while(pCount--) {

	// 	var particle = this.particles.vertices[pCount];

	// 	if(particle.position.y > .25 || particle.position.y < 0) {
	// 		particle.position.y = 0;
	// 		var radius =  this.sphereRadius*0.05;
	// 		var angle = Math.random() * (Math.PI * 2);
	// 		particle.position.x = Math.cos(angle) * radius;
	// 	}

	// 	//continue;
		
	// 	var t = Date.now() / 1000 % 3;
	// 	particle.position.x += Math.cos(t*particle.velocity.x) * .007;
	// 	particle.position.y += Math.sin(t*particle.velocity.y) * .05;

	// }
	this.particleSystem.geometry.__dirtyVertices = true;
}

// Constructor
ExhaustApp = function()
{
	Sim.App.call(this);
}

// Subclass Sim.App
goog.inherits( ExhaustApp, Sim.App );
ExhaustApp.prototype.ClassName = 'exhaust';

// Our custom initializer
ExhaustApp.prototype.init = function(param)
{
	// Call superclass init code to set up scene, renderer, default camera
	ExhaustApp.superClass_.init.call(this, param);
	
	
    // Create a headlight to show off the model
	this.headlight = new THREE.DirectionalLight( 0xffffff, 1);
	this.headlight.position.set(0, 0, 1);
	this.scene.add(this.headlight);	

	var exhaust = new Exhaust();
	exhaust.init();
	this.addObject(exhaust);

	this.camera.position.z = 3;
}

ExhaustApp.prototype.update = function()
{	
	ExhaustApp.superClass_.update.call(this);
}



window.onload =function() {

        var container = document.getElementById("container");
        
        var app = new ExhaustApp();
        app.init({ container: container });

        app.run();

    };